package com.spider.util;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.spider.entity.JiliEntity;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.LinkedHashMap;
import java.util.List;

public class JiLiUtil {

    public static String encode(String keyword) {
        if(keyword==null||keyword.equals("")) return null;
        try {
            keyword = URLEncoder.encode(keyword, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return keyword;
    }

    /**
     * 拼接修改价格信息
     * @return
     */
    public static JSONObject getUpdatePriceBody(List<JiliEntity> list){
        JSONObject body=new JSONObject(new LinkedHashMap());
        if(CollectionUtil.isEmpty(list)){
            return null;
        }
        body.put("name","orig_price");
        //拼接params
        JSONObject params=new JSONObject(new LinkedHashMap());
        //拼接items
        JSONArray items=new JSONArray();
        for(JiliEntity entity:list){
            JSONObject entityJb=new JSONObject(new LinkedHashMap());
            BigDecimal base=entity.getPrice();
            BigDecimal discount=base;
            if(entity.getCxbs()!=null&&entity.getCxbs().equals("不打折")){
                entityJb.put("brand_name","希恩思");
            }else{
                //判断产品类型不是氘代的
                if(entity.getType()!=null&&entity.getType().equals("氘代")){
                    entityJb.put("brand_name","希恩思");
                }else{
                    entityJb.put("brand_name","希恩思折扣");
                }
            }
            String gg=StringUtil.substringNum(entity.getNum())+entity.getUnit();
            entityJb.put("package",gg);
            entityJb.put("catalog_no",entity.getHs());
            entityJb.put("orig_price",discount);
            items.add(entityJb);
        }
        params.put("items",items);
        body.put("params",params);
        return body;
    }

    /**
     * 拼接新增产品信息
     * 编辑也使用此产品信息
     * @return
     */
    public static JSONObject insertProduceBody(List<JiliEntity> list){
        JSONObject body=new JSONObject(new LinkedHashMap());
        if(CollectionUtil.isEmpty(list)){
            return null;
        }
        body.put("name","batch-edit-spu");
        //拼接params
        JSONObject params=new JSONObject(new LinkedHashMap());
        //拼接items
        JSONArray items=new JSONArray();
        for(JiliEntity entity:list){
            JSONObject entityJb=new JSONObject(new LinkedHashMap());
            BigDecimal base=entity.getPrice();
            BigDecimal discount=base;
            if(entity.getCxbs()!=null&&entity.getCxbs().equals("不打折")){
                entityJb.put("brand_name","希恩思");
            }else{
                //判断产品类型不是氘代的
                if(entity.getType()!=null&&entity.getType().equals("氘代")){
                    entityJb.put("brand_name","希恩思");
                }else{
                    entityJb.put("brand_name","希恩思折扣");
                }
            }
            String gg=StringUtil.substringNum(entity.getNum())+entity.getUnit();
            //1 生物试剂，无CAS
            //2 通用试剂，有CAS
            if(StringUtil.isEmpty(entity.getCas())){
                //生物试剂
                params.put("type","bio_reagent");
                entityJb.put("category","其他实验试剂");
                entityJb.put("transport_cond",entity.getYunshufangshi());
                //存储条件 不设置
                entityJb.put("storage_cond",entity.getChucuntiaojian());
            }else{
                //化学试剂
                params.put("type","chem_reagent");
                entityJb.put("category","通用试剂");
                //以下为化学试剂单独需要添加的
                entityJb.put("cas_no",entity.getCas());
                entityJb.put("is_cylinder","否");
                if("g".equals(entity.getJibenunit())){
                    entityJb.put("state","固态");
                    entityJb.put("metric_weight",entity.getJibenNum()+entity.getJibenunit());
                }else{
                    entityJb.put("state","液态");
                    entityJb.put("metric_volume",entity.getJibenNum()+entity.getJibenunit());
                }
                entityJb.put("formula",entity.getFenzishi());
                entityJb.put("molecular_weight",entity.getFenziliang());
            }
            entityJb.put("action","编辑");
            entityJb.put("name",entity.getName());
            entityJb.put("package",gg);
            entityJb.put("catalog_no",entity.getHs());
            entityJb.put("orig_price",discount);
            //规则--纯度
            entityJb.put("spec",entity.getChundu());
            items.add(entityJb);
        }
        params.put("items",items);
        body.put("params",params);
        return body;
    }

    /**
     * 拼接删除产品信息
     * @return
     */
    public static JSONObject deleteProductBody(List<JiliEntity> list){
        JSONObject body=new JSONObject(new LinkedHashMap());
        if(CollectionUtil.isEmpty(list)){
            return null;
        }
        body.put("name","batch-edit-spu");
        //拼接params
        JSONObject params=new JSONObject(new LinkedHashMap());
        //拼接items
        JSONArray items=new JSONArray();
        for(JiliEntity entity:list){
            JSONObject entityJb=new JSONObject(new LinkedHashMap());
            if(entity.getCxbs()!=null&&entity.getCxbs().equals("不打折")){
                entityJb.put("brand_name","希恩思");
            }else{
                //判断产品类型不是氘代的
                if(entity.getType()!=null&&entity.getType().equals("氘代")){
                    entityJb.put("brand_name","希恩思");
                }else{
                    entityJb.put("brand_name","希恩思折扣");
                }
            }
            String gg=StringUtil.substringNum(entity.getNum())+entity.getUnit();
            //1 生物试剂，无CAS
            //2 通用试剂，有CAS
            if(StringUtil.isEmpty(entity.getCas())){
                //生物试剂
                params.put("type","bio_reagent");
            }else{
                //化学试剂
                params.put("type","chem_reagent");
            }
            entityJb.put("action","删除");
            entityJb.put("package",gg);
            entityJb.put("catalog_no",entity.getHs());
            items.add(entityJb);
        }
        params.put("items",items);
        body.put("params",params);
        return body;
    }
}
